package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public PageResult findMyLike(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        query.skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return new PageResult(page, pagesize, count, userLikes);
    }

    @Override
    public PageResult findLikeMe(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        query.skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return new PageResult(page, pagesize, count, userLikes);
    }

    @Override
    public PageResult findOtherLike(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> myLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> myLikeIds = myLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        Query query1 = Query.query(Criteria.where("likeUserId").in(userId).and("isLike").is(true));
        List<UserLike> likeMes = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeMeIds = likeMes.stream().map(UserLike::getUserId).collect(Collectors.toList());

        myLikeIds.retainAll(likeMeIds);

        Query query2 = Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").in(myLikeIds)
                .and("isLike").is(true));
        long count = mongoTemplate.count(query2, UserLike.class);
        query2.skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query2, UserLike.class);

        return new PageResult(page, pagesize, count, userLikes);
    }

    @Override
    public UserLike findOne(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("likeUserId").in(userId).and("userId").is(likeUserId).and("isLike").is(true));
        return mongoTemplate.findOne(query, UserLike.class);
    }

    @Override
    public void removeLike(Long userId, Long uid) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(uid));
        mongoTemplate.remove(query, UserLike.class);
    }

    /**
     * 统计喜欢相互喜欢粉丝数
     * 规则：如果两人互相喜欢则互为对方粉丝
     * @param userId
     * @return
     */
    /*@Override
    public Map count(Long userId) {

        //用户id为当前用户id的个数统计
        Query userIdQuery = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> loveCounts = mongoTemplate.find(userIdQuery, UserLike.class);

        //获取啊
        Integer loveCount = loveCounts.size();

        //查询互相喜欢的
        List<Long> longs = loveCounts.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        Query query = Query.query(Criteria.where("userId").in(longs).and("likeUserId").is(userId).and("isLike").is(true));
        long eachLoveCount = mongoTemplate.count(query, UserLike.class);
        Map map = new HashMap();
        map.put("loveCount",loveCount);
        map.put("eachLoveCount",eachLoveCount);
        map.put("fanCount",eachLoveCount);
        return map;
    }*/

    //统计互相喜欢
    @Override
    public Integer eachLoveCount(Long userId) {

        //1.查询出我喜欢用户的id集合
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> myLikeList = mongoTemplate.find(query1, UserLike.class);
        List<Long> myLikeUserIds = myLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        //2.查询喜欢我的用户的id集合
        Query query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> likeMeList = mongoTemplate.find(query2, UserLike.class);
        List<Long> likeMeUserIds = likeMeList.stream().map(UserLike::getUserId).collect(Collectors.toList());

        //3.求交集,返回交集个数
        myLikeUserIds.retainAll(likeMeUserIds);  //myLikeUserIds为求完交集后集合
        return myLikeUserIds.size();
    }

    //统计喜欢
    @Override
    public Integer loveCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.valueOf(count.toString());
    }

    //统计粉丝
    @Override
    public Integer fanCount(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.valueOf(count.toString());
    }

}
